package com.bwie.equipment.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bwie.equipment.entity.t_equipment;
import com.bwie.equipment.entity.t_factory;
import com.bwie.equipment.entity.t_status;
import com.bwie.equipment.entity.t_type;

public class EquipmentDaoImpl extends HibernateDaoSupport implements EquipmentDao{

	public List<t_factory> getfactoryList() {
		return getHibernateTemplate().find("from t_factory");
	}

	public List<t_status> getstatusList() {
		return getHibernateTemplate().find("from t_status");
	}

	public List<t_type> gettypeList() {
		return getHibernateTemplate().find("from t_type");
	}

	public void addEquipment(t_equipment equipment) {
		this.getHibernateTemplate().save(equipment);
	}

	public int getEquipmentCount(t_equipment equipment) {
		getHibernateTemplate().setCacheQueries(true);
		return (Integer) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery("select count(*) from t_equipment");
				query.setCacheable(true);
				Long count = (Long) query.uniqueResult();
				return count.intValue();
			}
		});
	}

	@SuppressWarnings("unchecked")
	public List<t_equipment> getEquipmentList(final t_equipment equipment) {
		getHibernateTemplate().setCacheQueries(true);
		return (List<t_equipment>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery("from t_equipment");
				query.setFirstResult(equipment.getStartPos());
				query.setMaxResults(equipment.getPageSize());
				query.setCacheable(true);
				return query.list();
			}
		});
	}

	public t_equipment getEquipmentByid(final t_equipment equipment) {
		getHibernateTemplate().setCacheQueries(true);
		return (t_equipment) this.getHibernateTemplate().get(t_equipment.class, equipment.getId());
	}

	public void updateEquipment(t_equipment equipment) {
		this.getHibernateTemplate().update(equipment);
	}

	public void deleteEquipment(t_equipment equipment) {
		equipment = (t_equipment) this.getHibernateTemplate().get(t_equipment.class, equipment.getId());
		this.getHibernateTemplate().delete(equipment);
	}


	
	
	
}
